//
//  JHMiddleListDetailViewController.m
//  JuniRengLish_iOS
//
//  Created by revogi on 2019/5/16.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHMiddleListDetailViewController.h"
#import <WebKit/WebKit.h>
#import "JHSettingFuntTopView.h"
#import "JHShareBottomView.h"

@interface JHMiddleListDetailViewController ()<WKUIDelegate,WKNavigationDelegate>
@property (nonatomic,strong) WKWebView *webView;
@property (nonatomic,copy) NSString *url;
@property (nonatomic,assign) NSInteger webFont;

@property (nonatomic,strong) JHSettingFuntTopView *popFuntView;
@property (nonatomic,strong) UIView *coverView;

@end

@implementation JHMiddleListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title=self.model.unitname;
    
    int founTag;
    NSString *fount=[kUserDefaults objectForKey:@"retFount"];
    if (fount) {
        self.webFont=[fount intValue];
    }else{
         self.webFont=2;  /// 0：小号字体，1：中号字体，2：大号字体  3:超大
    }
  
    
    [self initUI];
    [self initCoverView];
    [self setUpNav];
    //[self setUpBottom];
}

- (void)setUpNav
{
    UIButton*rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"字体设置" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(settingFuntBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}

- (void)initCoverView
{
    //蒙版的View
    self.coverView=[[UIView alloc]init];
    self.coverView.frame=CGRectMake(0, 0,JKScreenW, JKScreenH);
    self.coverView.backgroundColor=[UIColor blackColor];
    self.coverView.alpha=0.2;
    self.coverView.hidden=YES;
    [kAppWindow addSubview:self.coverView];

}


- (void)settingFuntBtnDidClick:(UIButton *)button
{
    WEAKSELF
    JHSettingFuntTopView *funtView=[[[NSBundle mainBundle]loadNibNamed:@"JHSettingFuntTopView" owner:self options:nil]lastObject];
    funtView.frame=CGRectMake(0, 0, JKScreenW, 130);
    self.popFuntView=funtView;
    self.coverView.hidden=NO;
    funtView.center=CGPointMake(JKScreenW/2, (JKScreenH-kTopHeight)/2);
    [kAppWindow addSubview:funtView];
    
    [self.popFuntView setSureBlock:^{
        weakSelf.coverView.hidden=YES;
        weakSelf.popFuntView.hidden=YES;
        NSString *fount=[kUserDefaults objectForKey:@"retFount"];
        if (fount) {
            weakSelf.webFont=[fount intValue];
        }
        [weakSelf initUI];
    }];
}

- (void)setUpBottom
{
    UIView *bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, JKScreenH-kTabBarHeight-kTopHeight-10, JKScreenW, 60)];
    bottomView.backgroundColor=[UIColor whiteColor];
    UIButton *shareBtn=[[UIButton alloc]initWithFrame:CGRectMake(30, 10,JKScreenW-60, 40)];
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    shareBtn.backgroundColor=JKMainColor;
    shareBtn.layer.cornerRadius=6.0;
    shareBtn.titleLabel.textColor=[UIColor whiteColor];
    [shareBtn addTarget:self action:@selector(shareBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:shareBtn];
    [self.view addSubview:bottomView];
}

#pragma mark-- - 分享
- (void)shareBtnDidClick:(UIButton *)button
{
    DXShareView *shareView = [[DXShareView alloc] init];
    DXShareModel *shareModel = [[DXShareModel alloc] init];
    [shareView showShareViewWithDXShareModel:shareModel shareContentType:DXShareContentTypeLink];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    WEAKSELF
    weakSelf.coverView.hidden=YES;
    weakSelf.popFuntView.hidden=YES;
    NSString *fount=[kUserDefaults objectForKey:@"retFount"];
    if (fount) {
        weakSelf.webFont=[fount intValue];
    }
    [weakSelf initUI];
    
}

- (void)initUI
{
    WKWebView *webView = [[WKWebView alloc] init];
    webView.frame =CGRectMake(0,0,JKScreenW, JKScreenH-kTopHeight);
    self.webView=webView;
    webView.UIDelegate=self;
    webView.navigationDelegate=self;
    [self.view addSubview:webView];
    self.url= [self.model.data.textfile stringByReplacingOccurrencesOfString:@".txt"withString:@""];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:self.url ofType:@"txt"];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSError *error = nil;
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error == nil) {
        [webView loadHTMLString:html baseURL:bundleUrl];
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    //禁止用户选择
    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
    if (self.webFont==0) {
        // 适当增大字体大小
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '150%'" completionHandler:nil];
    }else if(self.webFont==1){
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
    }else if(self.webFont==2){
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '250%'" completionHandler:nil];
    }
    else{
        [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'" completionHandler:nil];
    }
  
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
